package controllers;

import java.io.IOException;

import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;

import org.primefaces.model.StreamedContent;
import org.primefaces.util.Base64;

import services.ImageService;

@ManagedBean
@ApplicationScoped
public class ImageStreamer {

//    @EJB
    private ImageService imageService;
    private StreamedContent image;

/*    public StreamedContent getImage() throws IOException {
        FacesContext context = FacesContext.getCurrentInstance();

        if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
            // So, we're rendering the HTML. Return a stub StreamedContent so that it will generate right URL.
            return new DefaultStreamedContent();
        } else {
            // So, browser is requesting the image. Return a real StreamedContent with the image bytes.
            //String imageId = context.getExternalContext().getRequestParameterMap().get("imageId");
            //Menu image = imageService.find(Integer.valueOf(imageId));
            Menu imagen = imageService.find(8);
            image = new DefaultStreamedContent(new ByteArrayInputStream(imagen.getImagen()), "image/jpg");
            
   //         InputStream is = new ByteArrayInputStream((byte[]) yourBlobData);
   //         myImage = new DefaultStreamedContent(is, "image/png");
            
            return image;
        }
    }*/
    
    public StreamedContent getImage() throws IOException {
    	
/*    	 Menu imagen = imageService.find(8);
    	//byte[] imageByte = (byte[]) null;
    	BufferedImage imageBuffer = null;
    	//imageByte = byteToString(imageData);
    	ByteArrayInputStream in = new ByteArrayInputStream(imagen.getImagen());
    	imageBuffer = ImageIO.read(in);
    	ByteArrayOutputStream os = new ByteArrayOutputStream();
    	ImageIO.write(imageBuffer, "png", os);
    	image = new DefaultStreamedContent(new ByteArrayInputStream(os.toByteArray()),  "image/png");*/
    	return image;
    	}
    
    	public byte[] byteToString(String imageStr) {
    	return Base64.decode(imageStr);
    	}
	
    	public ImageService getImageService() {
			return imageService;
		}
		
    	public void setImageService(ImageService imageService) {
			this.imageService = imageService;
		}
		
    	public void setImage(StreamedContent image) {
			this.image = image;
		}


}

